package P1366;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Problema da disciplina de Estrutura de Dados e Complexidade de Algoritmos da
 * Pos Graduacao do Periodo 2011.1
 *
 * @see http://br.spoj.pl/problems/TV/
 * @author Elenilson Vieira - 111100371 - elenilson[at]elenilsonvieira.com
 * @since 16 de maio de 2011
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        int test = 1;
        StringBuilder arraysBuilder = new StringBuilder();

        while(!(line = reader.readLine()).trim().equals("0 0")){
            /*** Cria a matriz ***/
            String data[] = line.split(" ");
            int rowCount = Integer.parseInt(data[0]);
            int columnCount = Integer.parseInt(data[1]);
            String[][] array = new String[rowCount][columnCount];

            /*** Preenche a matriz ***/
            for (int i = 0; i < rowCount; i++)
                array[i] = reader.readLine().split(" ");

            int columnArrayBeginIndex = 0;
            int rowArrayBeginIndex = 0;
            
            /*** Pega sequencia de comandos e executa ***/
            while(!(line = reader.readLine()).trim().equals("0 0")){
                data = line.split(" ");
                int x = Integer.parseInt(data[0]);
                int y = -Integer.parseInt(data[1]);

                if (x > 0)
                   x = (x % columnCount) - columnCount;

                if (y > 0)
                   y = (y % rowCount) - rowCount;

                columnArrayBeginIndex = (columnArrayBeginIndex - x) % columnCount;
                rowArrayBeginIndex = (rowArrayBeginIndex - y) % rowCount;
            }

            arraysBuilder.append("Teste ").append(test++).append("\n");
            
            for (int row = rowArrayBeginIndex, j = 0; j < rowCount; row++, j++) {

                for (int column = columnArrayBeginIndex, i = 0; i < columnCount; column++, i++) {
                    arraysBuilder.append(array[row % rowCount][column % columnCount]).append(" ");
                }

                arraysBuilder.deleteCharAt(arraysBuilder.length() - 1).append("\n");
            }
            
            arraysBuilder.append("\n");
        }

        System.out.print(arraysBuilder.toString());
    }

}
